package top.lcpsky.flashcards.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import top.lcpsky.flashcards.entity.Todo;
import top.lcpsky.flashcards.mapper.TodoMapper;

@Service
public class TodoService {
    @Autowired
    private TodoMapper todoMapper;

    public List<Todo> getAllTodos(){
        List<Todo> res = todoMapper.getAll();
        if(!CollectionUtils.isEmpty(res)){
            for (Todo todo : res) {
                todo.setContentStr(new String(todo.getContent()));
            }
        }
        return res;
    }

    public boolean insert(Todo todo){
        return todoMapper.insert(todo)>0;
    }

    public boolean deleteByIds(Integer[] ids){
        boolean status = true;
        for (Integer id : ids) {
            boolean statusTemp = todoMapper.deleteByPrimaryKey(id)>0;
            status&=statusTemp;
        }
        return status;
    }

	/**   
	 * @Title: updateStatus   
	 * @param: @param id
	 * @param: @return      
	 * @return: boolean      
	 * @throws   
	 */
	public boolean updateStatus(Integer id) {
		Todo todo = todoMapper.selectByPrimaryKey(id);
		if (todo.getStatus().intValue()==1) {
			todo.setStatus((byte)0);
		}else {
			todo.setStatus((byte)1);
		}
		return todoMapper.updateByPrimaryKey(todo)>0;
	}
}